<?php
namespace Admin\Controller;

class CourseController extends BaseController {

    public function index() {

        $map['teacher_id'] = $this->admin['id'];
        $Course = D ('Course');
        $courseList = $Course->getCourseByWhere($map);

        $this->assign('courseList',$courseList);
        $this->display();
    }


    public function add() {

        if (!IS_POST) {
            $this->display();
        } else {
            $data['name'] = I ('request.name');
            $data['type'] = I ('request.type');
            $data['teacher_id'] = $this->admin['id'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $data['description'] = I ('request.description');

            $Course = D ('Course');
            $flag = $Course->_add($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit() {
        if (!IS_POST) {
            $id = I ('request.id');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Course = D ('Course');
            $c = $Course->_get($id);
            if (empty($c))
                $this->error('课程不存在');
            if ($c['teacher_id'] !== $this->admin['id'])
                $this->error('非法操作');

            $this->assign('course',$c);
            $this->display();
        } else {

            $data['id'] = I ('request.id');
            $data['name'] = I ('request.name');
            $data['teacher_id'] = $this->admin['id'];
            $data['type'] = I ('request.type');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $data['description'] = I ('request.description');

            $Course = D ('Course');
            $flag = $Course->_edit($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }


    public function del() {
        if (!IS_POST) $this->error('非法操作');

        $id = I ('request.id');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Course = D ('Course');
        $flag = $Course->_del($id);
        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

}